package day04;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.CookieUtil;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LoginServlet.service()");

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String savePassword=request.getParameter("savePassword");

        if("abc".equals(username)&&"123".equals(password)){
            //登录成功

            //选中了保存密码
            if("true".equals(savePassword)){
                CookieUtil.addCookie("username",username,response);
                CookieUtil.addCookie("password",password,response);
            }


            response.sendRedirect(request.getContextPath()+"/success.jsp");
            return;
        }

        //登录失败
        request.setAttribute("message","用户名或密码错误");
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request,response);

    }
}
